package com.chebaohui.iqs.service.impl.deal.libao.util;

import java.util.Date;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.service.impl.deal.libao.sdk.LibaoRequest;
import com.chebaohui.iqs.service.impl.deal.util.StringUtil;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.FormEncodingBuilder;

/**
 * 利宝认证处理
 * 
 * @author xielonghai
 *
 */
public class Certification {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(Certification.class);

	/**
	 * 利宝认证返回字段-tocken认证码(该字段每笔交易都需要传值)
	 */
	private static String access_token;
	/**
	 * 利宝认证返回字段-
	 */
	private static String token_type;
	/**
	 * 利宝认证返回字段-
	 */
	private static String refresh_token;
	/**
	 * 利宝认证返回字段-
	 */
	private static String expires_in;
	/**
	 * 利宝认证返回字段-
	 */
	private static String scope;

	/**
	 * 认证次数
	 */
	private static int tokenNum = 1;

	/**
	 * 认证时间
	 */
	private static long tokenTime = 0;

	static {
		if (goToken()) {
			log.info((tokenNum - 1) + "次，利宝认证成功....access_token:" + access_token);
		} else
			log.error((tokenNum - 1) + "次，利宝认证失败....");
	}

	/**
	 * 获取token
	 * 
	 * @return
	 */
	public static synchronized String getAccess_token() {
		if (StringUtil.isNulls(access_token)) {
			log.info("由于token认证码为空，重新进行验证....");
			if (goToken()) {
				log.info((tokenNum - 1) + "次，利宝认证成功....access_token:" + access_token);
				return access_token;
			} else {
				log.error((tokenNum - 1) + "次，利宝认证失败....");
				return "";
			}
		} else if (new Date().getTime() - tokenTime > LibaoConfig.dayToken) {
			log.info("半个月后发起token认证开始.....");
			if (goToken()) {
				log.info((tokenNum - 1) + "次，利宝认证成功....access_token:" + access_token);
				return access_token;
			} else {
				log.error((tokenNum - 1) + "次，利宝认证失败....");
				return "";
			}
		} else
			return access_token;
	}

	public static String getToken_type() {
		return token_type;
	}

	public static String getRefresh_token() {
		return refresh_token;
	}

	public static String getExpires_in() {
		return expires_in;
	}

	public static String getScope() {
		return scope;
	}

	/**
	 * 认证
	 * 
	 * @return
	 */
	private static synchronized boolean goToken() {
		try {
			tokenTime = new Date().getTime();
			log.info(tokenNum + "次，利宝发起认证....");
			RequestBody formBody = new FormEncodingBuilder().add("client_id", LibaoConfig.client_id)
					.add("client_secret", LibaoConfig.client_secret).add("grant_type", LibaoConfig.grant_type)
					.add("username", LibaoConfig.username).add("password", LibaoConfig.password).build();

			String retult = LibaoRequest.post(LibaoConfig.libao_url + "/oauth/token", formBody);
			log.info("利宝发起认证:"
			+"\n"+"请求路径:"+LibaoConfig.libao_url + "/oauth/token"
			+"\n"+"请求参数:"+formBody.toString()
			+"\n"+"返回数据:"+retult);

			if (StringUtil.isNulls(retult)) {
				log.error(tokenNum + "次，利宝认证失败...(返回null)");
				return false;
			}
			JSONObject json = JSON.parseObject(retult);
			access_token = json.getString("access_token");
			expires_in = json.getString("expires_in");
			refresh_token = json.getString("refresh_token");
			scope = json.getString("scope");
			token_type = json.getString("token_type");

			tokenNum++;
			return true;
		} catch (Exception e) {
			log.error(tokenNum + "次，利宝认证失败...(出现异常):" + e);
			return false;
		}
	}
}
